package org.light.oracle.complexverb;

import java.util.ArrayList;
import java.util.List;

import org.light.complexverb.TwoDomainVerb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Field;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.Type;
import org.light.oracle.generator.MybatisOracleSqlReflector;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class ListMyActive extends TwoDomainVerb{

	@Override
	public Method generateDaoImplMethod() throws Exception {
		Method method = new Method();
		method.setStandardName(StringUtil.capFirst(this.getVerbName()));
		method.setNoContainer(false);
		method.addSignature(new Signature(1, "db","*sql.DB"));
		method.addSignature(new Signature(2, this.master.getCapFirstDomainName()+"Id",this.master.getDomainId().getFieldType()));
		method.setReturnType(new Type("[] "+this.slave.getType()));
		
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,1,"sql := \""+MybatisOracleSqlReflector.generateSelectActiveUsingMasterIdStatement(master,slave)+"\""));
		sList.add(new Statement(2000L,1,"stmt, err := db.Prepare(sql)"));
		sList.add(new Statement(3000L,1,"if err != nil {"));
		sList.add(new Statement(4000L,2,"panic(err)"));
		sList.add(new Statement(5000L,1,"}"));
		sList.add(new Statement(6000L,1,"rows, err := stmt.Query("+this.master.getDomainId().getLowerFirstFieldName()+")"));
		sList.add(new Statement(7000L,1,"if err != nil {"));
		sList.add(new Statement(8000L,2,"panic(err)"));
		sList.add(new Statement(9000L,1,"}"));
		sList.add(new Statement(10000L,1,""+this.slave.getLowerFirstPlural()+" := [] "+this.slave.getDomainSuffix()+"."+this.slave.getCapFirstDomainName()+"{}"));
		sList.add(new Statement(11000L,0,""));
		sList.add(new Statement(12000L,1,"for rows.Next() {"));
		long serial = 13000L;
		StringBuilder sb = new StringBuilder();
		for (Field f:this.slave.getFields()) {
			sList.add(new Statement(serial,2,"var "+f.getLowerFirstFieldName()+" "+ f.getFieldType()));
			sb.append("&").append(f.getLowerFirstFieldName()).append(",");
			serial += 1000L;
		}
		String fieldsQuery = sb.toString();
		if (fieldsQuery.endsWith(",")) fieldsQuery = fieldsQuery.substring(0,fieldsQuery.length()-1);
		sList.add(new Statement(serial,0,""));
		sList.add(new Statement(serial+1000L,2,"err := rows.Scan("+fieldsQuery+")"));
		sList.add(new Statement(serial+2000L,2,"if err != nil {"));
		sList.add(new Statement(serial+3000L,3,"panic(err)"));
		sList.add(new Statement(serial+4000L,2,"}"));
		sList.add(new Statement(serial+5000L,2,"var "+this.slave.getLowerFirstDomainName()+" "+this.slave.getDomainSuffix()+"."+this.slave.getCapFirstDomainName()+""));
		serial += 6000L;
		for (Field f:this.slave.getFields()) {
			sList.add(new Statement(serial,2,""+this.slave.getLowerFirstDomainName()+".Set"+f.getCapFirstFieldName()+"("+f.getLowerFirstFieldName()+")"));
			serial += 1000L;
		}
		sList.add(new Statement(serial,1,"}"));
		sList.add(new Statement(serial+1000L,1,"return "+this.slave.getLowerFirstDomainName()+"s"));
		method.setMethodStatementList(WriteableUtil.merge(sList));
		return method;
	}
	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		Method method = new Method();
		method.setStandardName(StringUtil.capFirst(this.getVerbName()));
		method.setReturnType(new Type("Set",this.slave, this.slave.getPackageToken()));
		method.addAdditionalImport("java.util.Set");
		method.addAdditionalImport(this.slave.getPackageToken()+"."+this.slave.getDomainSuffix()+"."+this.slave.getCapFirstDomainNameWithSuffix());
		method.addSignature(new Signature(1,this.master.getLowerFirstDomainName()+"Id",this.master.getDomainId().getClassType()));
		method.setThrowException(true);
		return method;
	}
	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		Method method = new Method();
		method.setStandardName(StringUtil.capFirst(this.getVerbName()));
		method.setReturnType(new Type("Set",this.slave, this.slave.getPackageToken()));
		method.addAdditionalImport("java.util.Set");
		method.addAdditionalImport(this.slave.getPackageToken()+"."+this.slave.getDomainSuffix()+"."+this.slave.getCapFirstDomainNameWithSuffix());
		method.addSignature(new Signature(1,this.master.getLowerFirstDomainName()+"Id", this.master.getDomainId().getClassType()));
		method.setThrowException(true);
		return method;
	}
	@Override
	public Method generateServiceImplMethod() throws Exception {
		Method method = new Method();
		method.setStandardName(StringUtil.capFirst(this.getVerbName()));		
		method.setReturnType(new Type("Set",this.slave, this.slave.getPackageToken()));
		method.addSignature(new Signature(1,this.master.getLowerFirstDomainName()+"Id", this.master.getDomainId().getClassType()));
		method.addAdditionalImport("java.util.Set");
		method.addAdditionalImport("java.util.TreeSet");
		method.addAdditionalImport(this.slave.getPackageToken()+"."+this.slave.getDomainSuffix()+"."+this.slave.getCapFirstDomainNameWithSuffix());
		method.addAdditionalImport(this.master.getPackageToken()+"."+this.master.getDaoSuffix()+"."+this.master.getStandardName()+"Dao");
		method.addAdditionalImport(this.slave.getPackageToken()+"."+this.slave.getServiceSuffix()+"."+this.slave.getStandardName()+"Service");
		method.setThrowException(true);
		method.addMetaData("Override");
		
		Method daomethod = this.generateDaoMethodDefinition();
		
		List<Writeable> list = new ArrayList<Writeable>();
		list.add(new Statement(1000L,2,"Set<"+this.slave.getCapFirstDomainNameWithSuffix()+"> set = new TreeSet<"+this.slave.getCapFirstDomainNameWithSuffix()+">();"));
		list.add(new Statement(2000L,2,"set.addAll(dao."+StringUtil.lowerFirst(this.getVerbName())+"("+this.master.getLowerFirstDomainName()+"Id"+"));"));
		list.add(new Statement(3000L,2,"return set;"));
		method.setMethodStatementList(WriteableUtil.merge(list));
		return method;
	}
	@Override
	public Method generateControllerMethod() throws Exception {
		Method method = new Method();
		method.setStandardName(StringUtil.capFirst(this.getVerbName()));		
		method.setReturnType(new Type("Map<String,Object>"));
		method.setThrowException(true);
		method.addAdditionalImport("java.util.List");
		method.addAdditionalImport("java.util.Set");
		method.addAdditionalImport("java.util.TreeSet");
		method.addAdditionalImport(this.slave.getPackageToken()+"."+this.slave.getDomainSuffix()+"."+this.slave.getCapFirstDomainNameWithSuffix());
		method.addAdditionalImport(this.master.getPackageToken()+"."+this.master.getServiceSuffix()+"."+this.master.getStandardName()+"Service");
		method.addAdditionalImport(this.master.getPackageToken()+"."+this.master.getServiceimplSuffix()+"."+this.master.getStandardName()+"ServiceImpl");
		method.addSignature(new Signature(1,this.master.getLowerFirstDomainName()+"Id",this.master.getDomainId().getClassType(), this.slave.getPackageToken(),"RequestParam (required = false)"));
		method.addMetaData("RequestMapping(value = \"/"+StringUtil.lowerFirst(method.getStandardName())+"\", method = RequestMethod.POST)");

		List<Writeable> wlist = new ArrayList<Writeable>();
		wlist.add(new Statement(1000L,2,"Map<String,Object> result = new TreeMap<String,Object>();"));
		wlist.add(new Statement(2000L,2,"Set<"+this.slave.getCapFirstDomainNameWithSuffix()+"> set = new TreeSet<"+this.slave.getCapFirstDomainNameWithSuffix()+">();"));
		wlist.add(new Statement(3000L,2,"if ("+this.master.getLowerFirstDomainName()+"Id"+"!=null) set = service."+StringUtil.lowerFirst(this.getVerbName())+"("+this.master.getLowerFirstDomainName()+"Id"+");"));
		wlist.add(new Statement(4000L,2,"result.put(\"success\",true);"));
		wlist.add(new Statement(5000L,2,"result.put(\"rows\",set);"));
		wlist.add(new Statement(6000L,2,"return result;"));	
		method.setMethodStatementList(WriteableUtil.merge(wlist));
		
		return method;
	}
	public ListMyActive(Domain master,Domain slave){
		super();
		this.master = master;
		this.slave = slave;
		this.setVerbName("ListActive"+this.master.getCapFirstDomainName()+StringUtil.capFirst(this.slave.getAliasPlural())+"Using"+this.master.getCapFirstDomainName()+"Id");
		this.setLabel("列出所属");
	}

}